/**
 * @description 意见反馈
 */

import style from './styles/Feedback.less';
import classnames from "classnames";
import React from 'react';
import EntryBase from "../Common/EntryBase";
const Fragment = React.Fragment;
import { Button, Picker, Icon, InputItem, List, Toast } from 'wd-mobile';
import UploadImage from "components/UploadImage";

import { feedback } from "api_xukaiyu/app/content";

import util from "commons/util";
import config from 'commons/config';

class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            feedbackContent: "", // 反馈内容
            contactInfo: "", // 联系信息
            imgList: [], // 图片列表
        }
    }
    render() {
        return (
            <Fragment>
                <div className={classnames(style.main, "x-scroll-y")}>
                    <div className={classnames(style.contentAndImgs, "x-part")}>
                        <InputItem type="textarea" clearable={false} autoSize maxLength={300} placeholder="请输入您想要反馈的问题，我们将及时跟进。（300字以内）" onChange={(e) => {
                            this.setState({
                                feedbackContent: e.target.value
                            })
                        }} />
                        <UploadImage max={3} uploadUrl={`${config.public.rpcPath.xukaiyu}app/file_upload/upload_pic`} onChange={(imgList) => {
                            this.setState({
                                imgList: imgList
                            })
                        }} />
                    </div>
                    <div className={classnames(style.contact, "x-part")}>
                        <InputItem className={style.contactInput} title={<span className={style.contactTitle}>联系方式：</span>} type="phone" placeholder="留下联系方式，便于我们跟进反馈。" onChange={(e, s) => {
                            this.setState({
                                contactInfo: s.value
                            })
                        }} />
                    </div>
                    <div className={classnames(style.btn, "x-part")}>
                        <Button className={style.button} onClick={() => {
                            if (!this.state.feedbackContent) {
                                Toast.info("请输入您想要反馈的问题");
                                return;
                            }
                            if (!this.state.contactInfo) {
                                Toast.info("请留下联系方式");
                                return;
                            }
                            this.feedback();
                            return false;
                        }}>提交</Button>
                    </div>
                </div>
            </Fragment >
        )
    }

    // 反馈
    async feedback() {
        const toastLoading = await Toast.loading('提交中', 2000);
        feedback({
            feedbackContent: this.state.feedbackContent,
            contactInfo: this.state.contactInfo,
            imgList: this.state.imgList
        }).then(res => {
            toastLoading.destroy();
            Toast.info("提交成功", 1500, () => {
                util.checkWechatEnv((env)=>{
                    if(env=='miniprogram'){
                        wx.miniProgram.navigateBack();
                    }else{
                        history.back();
                    }
                })
                

            });
        }).catch(res => {
            toastLoading.destroy();
            Toast.info(res.message);
        })
    }
}

export default App;